package com.huatai.datacenter.service;

import com.alibaba.fastjson.JSONObject;
import com.huatai.datacenter.entity.ApiEntity;
import com.huatai.datacenter.entity.DataServicesEntity;
import com.huatai.datacenter.entity.DataSourceEntity;
import com.huatai.datacenter.mapper.DataServicesMapper;
import com.huatai.datacenter.utils.JDBCUtil;
import com.huatai.dataservice.dubbo.DataDisposeDubboAPI;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lion
 * @date 2023/6/18  18:57
 */
@Service
public class DataServices extends BaseService<DataServicesMapper, DataServicesEntity>{

	@Autowired
	private ApiService apiService;

	@Autowired
	private TopicTaskService taskService;

	@Autowired
	private HttpRequestService httpRequestService;

	@Autowired
	private DataModelApplyService dataModelApplyService;

	@Autowired
	private DataSourceService dataSourceService;

	@DubboReference
	private DataDisposeDubboAPI dataDisposeDubbo;

	/**
	 * @author Lion
	 * @description 获取实时任务数量、HTTP请求数量、api服务数量
	 * @param
	 * @return java.util.List<com.alibaba.fastjson.JSONObject>
	 */
	public List<JSONObject> queryData(){
		List<JSONObject> objectList = new ArrayList<>();

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("实时任务数量",taskService.count());
		objectList.add(jsonObject);

		JSONObject jsonObject1 = new JSONObject();
		jsonObject1.put("HTTP请求数量",httpRequestService.count());
		objectList.add(jsonObject1);

		JSONObject jsonObject2 = new JSONObject();
		jsonObject2.put("API数据服务数量",apiService.count());
		objectList.add(jsonObject2);

		return objectList;
	}

	/**
	 * @author Lion
	 * @description 获取所有实时的数据量和非实时的数据量
	 * @param
	 * @return java.util.List<com.alibaba.fastjson.JSONObject>
	 */
	public List<JSONObject> queryPieData(){

		List<JSONObject> objectList = dataDisposeDubbo.getAllTableCount();
		return objectList;
	}

	public void saveServiceApply(DataServicesEntity entity){
		if (entity.getModelId() != null){

			dataModelApplyService.saveDataModelApplyInstance(entity.getModelId(),entity.getModelName(),entity.getId(),entity.getServiceName(),"服务类数据模型");
		}
	}


}
